package com.zhuzhiguang.student.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhuzhiguang.student.entity.Favourite;
import com.zhuzhiguang.student.entity.Student;
import com.zhuzhiguang.student.service.StudentService;

@Controller
@RequestMapping("stu")
public class StudentController {
	// 测试  ====
	@Autowired
	StudentService stuService;
	
	@RequestMapping("list")
	public String list(Model model, @RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "3") int pageSize){
		
		List<Student> list = stuService.list(page, pageSize);
		model.addAttribute("list",list);
		return "list";
		
	}
	
	@RequestMapping("toadd")
	public String toAdd(Model model) {
		List<Favourite> favs = stuService.listFavs();
		model.addAttribute("favs",favs);
		return "add";
	}
	
	@RequestMapping("add")
	public String add(Student student) {
		System.out.print("stuent is " + student);
		stuService.add(student);
		return "redirect:list";
	}
	
	
	@RequestMapping("toupdate")
	public String toAdd(Model model,int id) {
		List<Favourite> favs = stuService.listFavs();
		model.addAttribute("favs",favs);
		
		// 获取当前学生的数据
		Student stu = stuService.getById(id);
		model.addAttribute("stu",stu);
		
		return "update";
	}
	
	@RequestMapping("update")
	public String update(Student student) {
		System.out.print("stuent is " + student);
		stuService.update(student);	
		return "redirect:list";
	}
	
	/**
	 * 删除
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("del")
	@ResponseBody
	public String del(Model model,int id) {
		stuService.delete(id);
		return "ok";
	}

}
